package io.xxx.omni.oss.custom

import com.google.gson.JsonObject
import io.xxx.omni.oss.core.CursorSynchronizer
import io.xxx.omni.oss.sdk.KsClient

/**
 * 快手
 */
// https://open.kwaixiaodian.com/docs/dev?pageSign=8cca5d25ba0015e5045a7ebec6383b741614263875756
abstract class KsSynchronizer : CursorSynchronizer<JsonObject>() {

    override fun hasNext(context: Context, response: JsonObject): Boolean {
        val cursor = getCursor(response)
        return cursor != "nomore"
    }

    override fun getResponse(context: Context, response: JsonObject?): JsonObject {
        val clientName = "${platform.id}Client${store.id}"
        val client = applicationContext.getBean(clientName, KsClient::class.java)
        val (method, params) = buildRequest(context, response)
        params["accessToken"] = store.authorization["accessToken"]
        params["apiMethodVersion"] = 1
        return client.execute(store, method, params)["data"].asJsonObject
    }

    protected abstract fun buildRequest(context: Context, response: JsonObject?): Pair<String, MutableMap<String, Any>>

    protected abstract fun getCursor(response: JsonObject): String
}